package com.cloudptt.api.product.func.call.single;

import android.content.Context;
import android.widget.Toast;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.pjsip.api.SipCallSession;
import com.cloudptt.api.product.config.call.AnswerModeConfig;
import com.cloudptt.api.product.func.IFunc;
import com.cloudptt.api.product.func.addr.IAddrOp;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.CallListener;
import com.cloudptt.api.product.func.call.ICallHistory;
import com.cloudptt.api.product.func.call.ICallMute;
import com.cloudptt.api.product.func.call.IIncommingCall;
import com.cloudptt.api.product.func.call.group.HalfCallInfo;
import com.cloudptt.api.product.func.call.group.fsm.call.GroupCallAgent;
import com.cloudptt.api.product.func.call.single.fsm.DisconnectingState;
import com.cloudptt.api.product.func.call.single.fsm.ICallState;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.CallOperator;
import com.cloudptt.api.product.parser.ErrParser;
import com.mcptt.config.model.BootStrapSvrInfo;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.db.model.AddrInfo;

import org.pjsip.pjsua2.pjsip_inv_state;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;



public class SingleCallTracker implements IFunc,IClearCall
{
	public final static String THIS_FILE = "SingleCallTracker";
	private final static int MAX__CALL_NUM = 1;

	private Context ctx = null;
	private McIdInfo mcIdInfo = null;
	private CallOperator operator = null;
	private ICallMute callMute = null;
//	private ICallNoSnd noSnd = null;
	private IAddrOp addrOp = null;
	private ICallHistory callHistory = null;
	private IIncommingCall incommingCall = null;

	private Class<?> activityCls = null;
	private Class<?> floatWindowServiceCls = null;
	private Object callMutex = new Object();
	private SingleCallBroadcast broadcast = null;
	private Map<String,SingleCallAgent> callMap = new ConcurrentHashMap<String,SingleCallAgent>();
	private AnswerModeConfig answerModeConfig = null;

	public SingleCallTracker(Context ctx, McIdInfo mcIdInfo, IAddrOp addOp, AnswerModeConfig answerModeConfig,CallOperator operator, ICallMute callMute)
	{
		this.ctx = ctx;
		this.mcIdInfo = mcIdInfo;
		this.addrOp = addOp;
		this.answerModeConfig = answerModeConfig;
		this.operator = operator;
		this.callMute = callMute;
//		this.noSnd = noSnd;
		this.broadcast = new SingleCallBroadcast(ctx,this);
	}


	public int newCall(CallAttr callAttr)
	{
		Log.d(THIS_FILE, "newCall  callMap.size() = " + callMap.size());
		if(callMap.size() >= MAX__CALL_NUM)
		{
			Set<String> key = callMap.keySet();
			for(Iterator<String> itor = key.iterator();itor.hasNext();)
			{
				String keyStr = itor.next();
				Log.d(THIS_FILE, "newCall  keyStr = " + keyStr);
				SingleCallAgent callAgent = callMap.get(keyStr);
				if(callAgent != null)
				{
					int state = callAgent.getState();
					Log.d(THIS_FILE, "newCall  state = " + state);
					if(state == CallConst.CallState_Activite)
					{
						return CauseCont.OperateCheck_Call_CallTooMany;
					}
					else
					{
						callAgent.destory();
						callMap.remove(callAgent);
					}
				}
			}
		}
		Log.d(THIS_FILE, "newCall  callAttr = " + callAttr);
		SingleCallAgent callAgent = new SingleCallAgent(ctx, callAttr, mcIdInfo, operator,this);
		if(activityCls == null
			|| floatWindowServiceCls == null)
		{
			Log.d(THIS_FILE, "newCall  cls = null");
		}
		callAgent.setPrivacyClass(activityCls,floatWindowServiceCls);
		String callKey = callAttr.getCallKey();
		Log.d(THIS_FILE, "newCall  callKey = " + callKey);
		callMap.put(callKey, callAgent);
//		setSnd();
		return CauseCont.OperateCheck_Suc;
	}

	public int doUserAction(String key,int userAction)
	{
		Log.d(THIS_FILE, "doUserAction userAction = " + CallConst.userActionTag(userAction));
		int result = CauseCont.OperateErr_NoAgent;
		SingleCallAgent callAgent = this.getCallAgent(key);
		if(callAgent != null)
		{
			if(userAction == CallConst.BtnAction_Accept
					|| userAction == CallConst.BtnAction_Reject)
			{
				if (incommingCall != null) {
					incommingCall.dlgClose(key);
				}
			}
			if(userAction == CallConst.BtnAction_Answer)
			{
				callMute.muteCall(true);
			}
			
			result = callAgent.doUserAction(userAction);
			Log.d(THIS_FILE, "doUserAction result = " + result);

			if(userAction == CallConst.BtnAction_Make
					&& CauseCont.OperateCheck_Suc != result)
			{
				this.clearAllCallAgent();
			}

			return result;
		}
		return result;
	}
	public boolean isHasCall(String callIdStr)
	{
		Log.d(THIS_FILE, "isHasCall callIdStr = " + callIdStr);
		if(callIdStr != null
				&& this.getCallAgent(callIdStr) != null)
		{
			return true;
		}
		return false;
	}

	//	public void clearCall(String callIdStr)
	//	{
	//		Log.d(THIS_FILE, "clearCall callIdStr = " + callIdStr);
	//		if(callIdStr != null
	//				&& callMap != null)
	//		{
	//			WebRtcCallAgent callAgent = callMap.remove(callIdStr);
	//			Log.d(THIS_FILE, "clearCall callAgent = " + callAgent);
	//			callAgent.destory();
	//			callAgent = null;
	//		}
	//	}

	//	@Override
	//	public void receiveCallAction(int callId,int talkAction) {
	//		// TODO Auto-generated method stub
	//		Log.d(THIS_FILE, "receiveCallAction talkAction = " + talkAction);
	//		if(curCallAgent != null)
	//		{
	//			curCallAgent.receiveCallAction(callId,talkAction);
	//			if(GroupCallConst.Session_Disconnected == talkAction)
	//			{
	//				curCallAgent.doExit();
	//				curCallAgent = null;
	//				//				callMap.clear();
	//			}
	//		}
	//
	//	}

	public void incomingCall(SipCallSession callSession)
	{
		if(callSession == null)
		{
			return;
		}
		Log.d(THIS_FILE, "incomingCall callSession = " + callSession);

		String imcomingNum = callSession.getRemoteNum();
		int callType = callSession.getCallType();
		if(CallAttr.isGroupCall(callType))
		{
			return;
		}
		AddrInfo imcomingInfo = addrOp.getUserInfoByNum(imcomingNum);
		String callIdStr = callSession.getCallIdStr();
		String callKey = callSession.getCallKey();
		SingleCallAgent oldCallAgent = this.getCallAgent(callKey);
		if(oldCallAgent != null)
		{
			Log.debug(this.getClass(), "oldCallAgent exist");
//			String str = Resource.getString("group_has_call");//ctx.getString(R.string.null_group);
//			CommDlg.showShortToast(ctx, str);
			operator.hangupCall(callIdStr,486);
			return;
		}

		if(callMap.size() > 0)
		{
			Log.d(THIS_FILE, "incomingCall callMap.size() > 0");
			operator.hangupCall(callIdStr,486);
			return;
		}
		List<HalfCallInfo> callInfos = callMute.getGroupCallInfoList();
		if(callInfos != null
			&& callInfos.size() > 0)
		{
			Log.d(THIS_FILE, "incomingCall groupcall > 0");
			operator.hangupCall(callIdStr,486);
			return;
		}

		CallAttr callAttr = new CallAttr(callType,imcomingInfo,CallAttr.CallDir_MT,callSession.hasVideo(),callSession.isEmergency(),"","");
		callAttr.updateCallIdStr(callIdStr);

		int result = newCall(callAttr);
		Log.d(THIS_FILE, "incomingCall result = " + result + " callAttr = " + callAttr);

		SingleCallAgent callAgent = this.getCallAgent(callAttr.getCallKey());
		if(callAgent == null)
		{
			Log.d(THIS_FILE, "incomingCall callAgent == null");
			operator.hangupCall(callIdStr,486);
			return;
		}

		int callState = callAgent.getState();
		Log.d(THIS_FILE, "incomingCall callIdStr = " + callIdStr + " callState = " + callState + " callType = " + callType + " answerModeConfig = " + answerModeConfig);
		if(CallConst.CallState_InitState == callState
				&& result == CauseCont.OperateCheck_Suc)
		{
			if(answerModeConfig != null)
			{
				boolean isAutoAnswer = answerModeConfig.isAutoAnswer(callType, callAttr.isHasVideo());
				Log.debug(this.getClass(), "incomingCall isAutoAnswer = " + isAutoAnswer);
				if (isAutoAnswer)
				{
					callAgent.goToState(CallConst.CallState_ConnectingState);
				}
				else
				{
					if (operator != null) {
						operator.ring(ctx,true);
						operator.answer180(callIdStr, callSession.mediaHasVideo());
					}
					if (incommingCall != null) {
//				callAgent.receiveCallAction(CallConst.Session_Connecting);
						Log.debug(this.getClass(), "incomingCall goto CallState_WattingState ");
						callAgent.goToState(CallConst.CallState_WattingState);
						LaunchCallInfo callInfo = callAttr.toLaunchCallInfo(false, callState, CallConst.CallMediaState_None, true);
						incommingCall.incommingCall(imcomingInfo, callInfo);
					}
				}
			}
		}
		else
		{
			result = operator.hangupCall(callIdStr,486);
			if(result != CauseCont.OperateCheck_Suc) 
			{
				String errStr = CauseCont.getOperateCheckErrStr(result,ctx);
				CommDlg.showToast(ctx,errStr);
				return;
			}
		}
	}

	public void recvPCallSession(SipCallSession callSession)
	{
		Log.d(THIS_FILE, "recvPCallSession into ");
		synchronized (callMutex) 
		{
			Log.d(THIS_FILE, "recvPCallSession callSession is " + callSession.toString());
			int callState = callSession.getCallState();
			String callIdStr = callSession.getCallIdStr();
			Log.d(THIS_FILE, "recvPCallSession callIdStr = " + callIdStr);
//			if(callState >= pjsip_inv_state.PJSIP_INV_STATE_CALLING)
//			{
//				updateCallKeyForInitCall(callIdStr);
//			}

			String key = callSession.getCallKey();
			SingleCallAgent callAgent = this.getCallAgent(key);
			if(callAgent == null)
			{
				Log.d(THIS_FILE, "recvPCallSession callAgent == null");
				operator.hangupCall(callIdStr,486);
				return;
			}

			String oldCallIdStr = callAgent.getCurCallIdStr();
			Log.d(THIS_FILE, "recvPCallSession oldCallIdStr = " + oldCallIdStr);
			if(oldCallIdStr != null)
			{
				if(oldCallIdStr.equals(CallAttr.DEF_CALL_ID_STR)) {
					callAgent.updateCallIdStr(callIdStr);
				}
				else if(!oldCallIdStr.equals(callIdStr))
				{
					return;
				}
			}
			int state = callSession.getCallState();
			int lastCode = callSession.getLastStatusCode();
//			if(InvState.EARLY == state
//					|| InvState.CONFIRMED == state)
//			{
//				callAgent.sendLocalCandidateByMo();
//			}
			if (pjsip_inv_state.PJSIP_INV_STATE_DISCONNECTED == state) {
				if (incommingCall != null) {
					incommingCall.dlgClose(key);
				}
			}
			if(pjsip_inv_state.PJSIP_INV_STATE_CONFIRMED == state)
			{

				Log.d(THIS_FILE, "recvPCallSession lastCode is " + lastCode +" remote = "+callSession.getRemoteContact()  +" callKey = "+callSession.getCallKey());
				if(SipCallSession.StatusCode.OK == lastCode)
				{
					if(callAgent != null)
					{
						boolean hasVideo = true;
						Log.d(THIS_FILE, "recvPCallSession cachedVideo="+callSession.mediaHasVideo() );
						if (callSession.mediaHasVideo()) {
							hasVideo = true;
						}else {
							hasVideo = false;
						}
						Log.d(THIS_FILE, "recvPCallSession hasVideo =" + hasVideo );
						callAgent.setHaseVideo(hasVideo);
						callAgent.receiveCallAction(CallConst.Session_Confirmed_Active);
					}
					
				}
				else
				{
					int callDir = callAgent.getCallAttr().getCallDir();
					int code = callSession.getLastStatusCode();
					showErrToast(callDir,code,ErrParser.Type_FloorResp);
				}

//				CallAttr callAttr = callAgent.getCallAttr();
//				int callType = callAttr.getCallType();
//				if(CallAttr.CallType_Single == callType)
//				{
//					setNoSnd();
//				}
			}
			else if(pjsip_inv_state.PJSIP_INV_STATE_DISCONNECTED == state)
			{
				//					curCallSession = null;
				callAgent.receiveCallAction(CallConst.Session_Disconnected);
				Log.d(THIS_FILE, "recvPCallSession DISCONNECTED callAgent.getState() = " + callAgent.getState());
				int callDir = callAgent.getCallAttr().getCallDir();
				int code = callSession.getLastStatusCode();
				if(code == 403)
				{
					String errMsg = callSession.getErrMsg();
					String subErrMsg = callSession.getErrSubMsg();
					if(errMsg != null
							&& subErrMsg != null)
					{
						showFuncErrToast(errMsg,subErrMsg);
					}
					else
					{
						showErrToast(callDir,code,ErrParser.Type_CallResp);
					}
				}
				else
				{
					showErrToast(callDir,code,ErrParser.Type_CallResp);
				}

//				CallAttr callAttr = callAgent.getCallAttr();
//				AddrInfo receiver = callAttr.getCalled();
//				AddrInfo sender = callAttr.getCallLogAddrInfo(mcIdInfo);
//				String secStr = callAgent.getTimeDesc();
//				newCallHistory(receiver,sender,callDir,code,secStr);

//				clearAllCallAgent();
				SingleCallAgent agent = removeCallAgent(key);
				ICallState currentState = new DisconnectingState(ctx,agent);
				agent.setCurrentState(currentState);
				agent.destory();
				agent= null;

				
				callMute.muteCall(false);
//				setNoSnd();
//				int callType = callAttr.getCallType();
//				if(CallAttr.CallType_Single == callType)
//				{
//					//setSnd();
//				}
			}
		}
	}

	public void receivePCallMediaStatus(String callIdStr,int mediaStatus)
	{
		Log.d(THIS_FILE, "receivePCallMediaStatus callIdStr = " + callIdStr +" mediaStatus = " + mediaStatus);
		SingleCallAgent callAgent = getCallAgentByCallIdStr(callIdStr);
		if (callAgent == null) {
			Log.d(THIS_FILE, "receivePCallSession callAgent == null");
			return;
		}
		callAgent.receivePCallMediaStatus(mediaStatus);
	}

	public void receivePCallVideoEvent(String callIdStr)
	{
		Log.d(THIS_FILE, "receivePCallVideoEvent callIdStr = " + callIdStr);
		SingleCallAgent callAgent = getCallAgentByCallIdStr(callIdStr);
		if (callAgent == null) {
			Log.d(THIS_FILE, "receivePCallSession callAgent == null");
			return;
		}
		callAgent.receivePCallVideoEvent();
	}
//	private void newCallHistory(AddrInfo receiver,AddrInfo sender,int callDir,int code,String secStr)
//	{
//		boolean isAdd = false;
//		StringBuffer contentStr = new StringBuffer();
//		if(secStr != null)
//		{
//			String str = Resource.getString("e_log_suc");
//			contentStr.append(str).append(secStr);
//		}
//		else
//		{
//			if(code == 403
//					|| code == 408
//					|| code == 486
//					|| code == 487
//					|| code == 603)
//			{
//				ErrInfo errInfo = new ErrInfo(CauseCont.ERROR_TITLE_LOG, code);
//				if( errInfo != null )
//				{
//					String str = errInfo.getErrDesc();
//					contentStr.append(str);
//				}
//			}
//			else
//			{
//				String str = Resource.getString("e_log_err");
//				contentStr.append(str + code);
//			}
//
//
//			if(code == 408
//					|| code == 487)
//			{
//				isAdd = true;
//			}
//		}
//		int dir = SessionInfo.Direction_Type_Send;
//		if(callDir == CallAttr.CallDir_MT)
//		{
//			dir = SessionInfo.Direction_Type_Receive;
//		}
//		callHistory.newCallHistory(receiver, sender,dir,isAdd,contentStr.toString());	
//	}

	private void showErrToast(int callDir,int code,int type)
	{
		Log.d(THIS_FILE, "showErrToast callDir = " + callDir + " code = " + code + " type = " + type);
		ErrParser errParser = new ErrParser(code, type, ctx);
		if(errParser.isErr() )
		{
			if(code == 486
					&& callDir == CallAttr.CallDir_MT)
			{
				return;
			}
			String errStr = errParser.getErrStr();
			if(errStr != null
					&& !errStr.equals(""))
			{
				CommDlg.showToast(ctx, errStr);
			}
		}
	}

	private void showFuncErrToast(String errMsg,String subErrMsg)
	{
		Log.d(THIS_FILE, "showFuncErrToast errMsg = " + errMsg + " subErrMsg = " + subErrMsg);
		if(errMsg != null
			&& subErrMsg != null)
		{
			String errStr = errMsg + ":" + subErrMsg;
			if(errStr != null
					&& !errStr.equals(""))
			{
				CommDlg.show(ctx, errStr, Toast.LENGTH_LONG);
			}
		}
	}

	public String getCallDesc()
	{
		int pcallSize = 0;
		int otherCallSize = 0;
		Set<String> keySet = callMap.keySet();
		for(Iterator<String> itor = keySet.iterator();itor.hasNext();)
		{
			String key = itor.next();
			SingleCallAgent agent = callMap.get(key);
			CallAttr attr = agent.getCallAttr();
			if(attr != null
					&& attr.isPrivateCall())
			{
				pcallSize ++;
			}
			else 
			{
				otherCallSize ++;
			}
		}
		return "" + otherCallSize + pcallSize;
	}

	public List<CallInfo> getCallInfoList()
	{
		Log.d(THIS_FILE,"getCallInfoList" );
		List<CallInfo> list = new Vector<CallInfo>();
		Set<String> keySet = callMap.keySet();
		for(Iterator<String> itor = keySet.iterator();itor.hasNext();)
		{
			String key = itor.next();
			SingleCallAgent agent = callMap.get(key);
			if(agent != null) {
				CallInfo info = agent.getCallInfo();
				list.add(info);
			}
		}
		return list;
	}

	private SingleCallAgent getCallAgent(String key)
	{
		Log.d(THIS_FILE, "getCallAgent  key = " + key);
		Log.d(THIS_FILE, "getCallAgent  callMap = " + callMap);
		if(callMap == null
				|| callMap.size() <=0)
		{
			return null;
		}
		
		return callMap.get(key);
	}

	private SingleCallAgent getCallAgentByCallIdStr(String callIdStr)
	{
		Log.debug(this.getClass(), "getCallAgentByCallIdStr  callIdStr = " + callIdStr);
		if(callMap == null
				|| callMap.size() <=0)
		{
			return null;
		}
		Log.debug(this.getClass(), "getCallAgentByCallIdStr  callMap = " + callMap);
		Set<String> keySet = callMap.keySet();
		Log.debug(this.getClass(), "getCallAgentByCallIdStr  keySet = " + keySet);
		for(Iterator<String> itor = keySet.iterator();itor.hasNext();)
		{
			String key = itor.next();
			Log.debug(this.getClass(), "getCallAgentByCallIdStr  key = " + key);
			SingleCallAgent callAgent = callMap.get(key);
			if(callAgent != null) {
				String oldCallIdStr = callAgent.getCurCallIdStr();
				Log.debug(this.getClass(), "getCallAgentByCallIdStr  callIdStr = " + callIdStr + " oldCallIdStr = " + oldCallIdStr);
				if (oldCallIdStr != null
						&& oldCallIdStr.equals(callIdStr)) {
					return callAgent;
				}
			}
		}
		return null;
	}

	private SingleCallAgent removeCallAgent(String key)
	{
		Log.d(THIS_FILE, "removeCallAgent  key = " + key);
		return callMap.remove(key);
	}



	public boolean isNewCall(int state)
	{
		if(state == pjsip_inv_state.PJSIP_INV_STATE_CALLING
				|| state == pjsip_inv_state.PJSIP_INV_STATE_INCOMING
				|| state == pjsip_inv_state.PJSIP_INV_STATE_CONNECTING)
		{
			return true;
		}
		return false;
	}


	public void setPrivacyClass(Class<?> activityCls,Class<?> floatWindowServiceCls)
	{
		Log.d(THIS_FILE, "setPrivacyClass activityCls = " + activityCls + " floatWindowServiceCls = " + floatWindowServiceCls);
		this.activityCls = activityCls;
		this.floatWindowServiceCls = floatWindowServiceCls;
	}


	public void setListener(CallListener callListener)
	{
		Log.d(THIS_FILE, "setListener callListener = " + callListener);
		Set<String> keySet = callMap.keySet();
		for(Iterator<String> itor = keySet.iterator();itor.hasNext();)
		{
			String key = itor.next();
			SingleCallAgent agent = callMap.get(key);
			agent.setListener(callListener);
		}
	}

	public void setShowFloatWin(String callKey, boolean isLaucher)
	{
		Log.d(THIS_FILE, "setShowFloatWin callKey = " + callKey + " isLaucher = " + isLaucher);
		SingleCallAgent agent = callMap.get(callKey);
		if(agent != null)
		{
			agent.setShowFloatWin(isLaucher);
		}
	}

	//	public void setSurfaceView(String callIdStr, SurfaceViewRenderer localRender, SurfaceViewRenderer remoteRender) {
	//		// TODO Auto-generated method stub
	//		WebRtcCallAgent callAgent = this.getCallAgent(callIdStr);
	//		if(callAgent != null)
	//		{
	//			callAgent.setSurfaceView(localRender, remoteRender);
	//		}
	//	}
//	public SurfaceViewRenderer getSurfaceView(String callIdStr, int type)
//	{
//		SingleCallAgent callAgent = this.getCallAgent(callIdStr);
//		if(callAgent != null)
//		{
//			return callAgent.getSurfaceView(type);
//		}
//		return null;
//	}
	public void setIslaunch(String callKey, boolean islaunch)
	{
		SingleCallAgent callAgent = this.getCallAgent(callKey);
		if(callAgent != null)
		{
			callAgent.setIslaunch(islaunch);
		}
	}

	//	public GroupCallInfo getCallInfo(String callIdStr)
	//	{
	//		WebRtcCallAgent callAgent = this.getCallAgent(callIdStr);
	//		if(callAgent != null)
	//		{
	//			return callAgent.getCallInfo();
	//		}
	//		return null;
	//	}
	//	public List<GroupCallInfo> getTalkInfoList(int callType)
	//	{
	//		List<GroupCallInfo> list = new Vector<GroupCallInfo>();
	//		Set<String> keySet = callMap.keySet();
	//		for(Iterator<String> itor = keySet.iterator();itor.hasNext();)
	//		{
	//			String key = itor.next();
	//			WebRtcCallAgent agent = callMap.get(key);
	//			GroupCallInfo info = agent.getCallInfo();
	//			list.add(info);
	//		}
	//		return list;
	//	}

	public void destory() {
		Log.d(THIS_FILE, "destory");
		Set<String> keySet = callMap.keySet();
		for(Iterator<String> itor = keySet.iterator();itor.hasNext();)
		{
			String key = itor.next();
			SingleCallAgent agent = callMap.get(key);
			agent.destory();
		}
		//		callAttr = null;
	}

	//	@Override
	//	public boolean isActiveCall() {
	//		// TODO Auto-generated method stub
	//		if(curCallAgent != null)
	//		{
	//			return curCallAgent.isActiveCall();
	//		}
	//		return false;
	//	}

	//	@Override
	//	public int getState() {
	//		// TODO Auto-generated method stub
	//		return 0;
	//	}
	@Override
	public void onLanChange(String lanType) {
		// TODO Auto-generated method stub

	}
	@Override
	public void onLogined(McIdInfo mcIdInfo, BootStrapSvrInfo info, boolean isLoginSuc)
	{}
	@Override
	public void onPublishResult(int publishResult)
	{}
	@Override
	public void onSubscribeCmsGmsResult(boolean isSuc,int mcType)
	{}
	@Override
	public void doNetworkChange(boolean isConn)
	{
		Log.d(THIS_FILE, "doNetworkChange isConn = " + isConn);
		if(!isConn)
		{
			clearAllCallAgent();
		}
	}
	@Override
	public void doForceDetach()
	{
		Log.d(THIS_FILE, "doForceDetach");
		clearAllCallAgent();
	}
	@Override
	public void doLogout()
	{
		Log.d(THIS_FILE, "doLogout");
		clearAllCallAgent();
	}
	@Override
	public void doExit()
	{
		Log.d(THIS_FILE, "doExit");
		clearAllCallAgent();
		this.broadcast.clearResource();
		broadcast = null;
	}

	//	public CallOperator getOperator() {
	//		return operator;
	//	}

	//	public CallAgent getCallAgent() {
	//		return curCallAgent;
	//	}

	public String toString() 
	{
		StringBuffer strBuff = new StringBuffer("McpttCall:");
		//		strBuff.append("callAttr = ").append(callAttr).append(",");
		//		strBuff.append("callAgent = ").append(callAgent).append(";");

		return strBuff.toString();
	}

	@Override
	public void onKeepAlive(boolean isconnected) {
		// TODO Auto-generated method stub
		if(!isconnected)
		{
			clearAllCallAgent();
		}
	}

	public void clearAllCallAgent()
	{
		try
		{
		Log.d(THIS_FILE, "clearAllCallAgent");
		Set<String> keySet = callMap.keySet();
		for(Iterator<String> itor = keySet.iterator();itor.hasNext();)
		{
			String key = itor.next();
			SingleCallAgent agent = callMap.remove(key);
			agent.destory();
			agent = null;
		}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}


	public boolean isHaveActivePCall()
	{
		Log.d(THIS_FILE, "isHaveActivePCall");
		Set<String> keySet = callMap.keySet();
		for(Iterator<String> itor = keySet.iterator();itor.hasNext();)
		{
			String key = itor.next();
			SingleCallAgent agent = callMap.get(key);
			if(agent.isHaveActivePCall())
			{
				return true;
			}
		}
		return false;
	}
	
	public boolean isHaveActivePVoiceCall()
	{
		Log.d(THIS_FILE, "isHaveActivePVoiceCall");
		Set<String> keySet = callMap.keySet();
		for(Iterator<String> itor = keySet.iterator();itor.hasNext();)
		{
			String key = itor.next();
			SingleCallAgent agent = callMap.get(key);
			
			if(agent.isHaveActivePVoiceCall())
			{
				return true;
			}
		}
		return false;
	}
	public boolean setSpeakerphoneEnable(String callKey, boolean enable)
	{
		return operator.setSpeakerphoneEnable(enable);
	}
	public boolean setMicrophoneEnable(String callKey, boolean enable)
	{
		SingleCallAgent callAgent = this.getCallAgent(callKey);
		if(callAgent != null)
		{
			return  callAgent.setMicrophoneEnable(enable);
		}
		return false;
//		return operator.setMicrophoneEnable(callKey,enable);
	}

	public boolean isActiveCall(String callKey) {
		// TODO Auto-generated method stub
		SingleCallAgent callAgent = this.getCallAgent(callKey);
		if(callAgent != null)
		{
			return callAgent.isActiveCall();
		}
		return false;
	}

	public void setCallHistory(ICallHistory callHistory) {
		this.callHistory = callHistory;
	}
	
	public void screenAngle(String callKey, int angle)
	{
		SingleCallAgent callAgent = this.getCallAgent(callKey);
		if(callAgent != null)
		{
			callAgent.screenAngle(angle);
		}
	}
	public void setIncommingCall(IIncommingCall incommingCall) {
		this.incommingCall = incommingCall;
	}
//	private void setNoSnd()
//	{
//		Log.d(THIS_FILE, "agent setNoSnd");
//		Message msg = mHandler.obtainMessage(MsgId_SetNoSnd);
//		mHandler.sendMessage(msg);
//	}
//
//	private void setSnd()
//	{
//		Log.d(THIS_FILE, "agent setSnd ");
//		Message msg = mHandler.obtainMessage(MsgId_SetSnd);
//		mHandler.sendMessage(msg);
//	}
//	
//	private final static int MsgId_SetNoSnd = 2;
//	private final static int MsgId_SetSnd = 3;
//	private  Handler mHandler = new Handler(){ 
//		@Override
//		public void handleMessage(Message msg)
//		{
//			Log.d(THIS_FILE, "handleMessage msg.what = " + msg.what);
//			if(msg.what == MsgId_SetNoSnd)
//			{
//				try {
//					noSnd.setNoSnd();
//				} catch (Exception e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//			else if(msg.what == MsgId_SetSnd)
//			{
//				try {
//					noSnd.setSnd(true);
//				} catch (Exception e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//		}
//	};
}
